package lab03;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Test07 {
    public static void main(String[] args) {
        String path = "G:\\12321.zip";
        try {
            ZipFile zipFile = new ZipFile(path);

            //Enumeration是枚举的意思
            Enumeration entries = zipFile.entries();
            ZipEntry zipEntry = null;

            //遍历：
            while(entries.hasMoreElements()){
                zipEntry = (ZipEntry)entries.nextElement();
                System.out.println(zipEntry.getName());
            //创建文件夹
                File testFlie = new File("G:\\test");
                testFlie.mkdir();//创建文件夹

                //字节流读文件写文件
                InputStream fileInputStream = zipFile.getInputStream(zipEntry);
                FileOutputStream fileOutputStream = new FileOutputStream(
                        new File("G:\\test\\"+zipEntry.getName()));

                        byte[] data = new byte[1024];
                        while (fileInputStream.read(data) != -1){
                            fileOutputStream.write(data);
                        }



            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* ZipInputStream zip;//创建ZipInputStream的对象
        try {//用try语句捕捉可能出现的异常
            zip = new ZipInputStream(new FileInputStream("G://12321.zip"));
            //实例化对象，指明要解压的对象
            ZipEntry entry =zip.getNextEntry();//获取下一个ZipEntry()
            if (((entry = zip.getNextEntry())!=null)&&!entry.isDirectory()){//若果entry不为空，且不再同一个目录下
                    File file =new File("G://"+entry.getName());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }
}
